% extracts the data from the excell files, converts the values of the
% labels from pixeis to coordinates (like worm position), normalizes the
% position in relation to a defined position for the OP stimuli so that all
% worms have the stimulus torwards they are heading in the same place
% OP Stimulus x=17500 y=12500 PA stimulus x=7500 y=12500

%PUTS TOGETHER ALL INDIVIDUALS IN A GROUP OF EXPERIMENTS

%% extra functions 
%convertx
%converty

%%
% DON'T FORGET TO CHANGE NUMBER OF WORMS (24) AND NAME IN THE END (line 90) 
% ANALYZIS DESIGN FOR OP STIMULI (RIGHT SIDE OF IMAGE);
%%
clear all
load ('/Users/anapereira/Dropbox/Matlab/data/revlab.mat');

% stores the data from the excell file 
store = cell (1,30,1);

% select the number of worms you are analyzing at the experiment
nworms = 3; %modify this value at the begining of the analyzis

% select the excell file from where to extract the data
for i =1:nworms
    [filename, pathname] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},'File Selector');

    number = xlsread (filename,1);
    time = xlsread (filename ,2);
    xpos = xlsread (filename,3);
    ypos = xlsread (filename,4);
    [num, text, raw] = xlsread (filename,6);% to extract both numeric and string data
    reversals = raw (:,1);
    [num1, text1, raw1] = xlsread (filename,7);
    omegas = raw1 (:,1);
    label_x = xlsread (filename,10);
    label_y = xlsread (filename,11);
    time_to_arrive = xlsread (filename,12);
    conversion = xlsread (filename,14);

    %% transform the value of the labels from pixeis to coordinates and store on
    %the cell array
    xlab = convertx (label_x,conversion);
    ylab = converty (label_y, conversion);

    %% find the parameters to align to the PA stimuli
    %set the same value for the position of the PA stimuli - I decided these
    %values in an arbitrary way to fit well in the graph
    OPx = 17500;
    OPy = 12500;

    %find the distance between where the stimulus is in my video and the specificed PA stimuli
    xdistOP = OPx - xlab;
    ydistOP = OPy - ylab;

    %% correct the values of x and y torwards the new stimulus values
    n = size (xpos, 1);
    horfinal = [];%for x values horizontally
    for j = 1:n;
        hornorm = xpos (j) + xdistOP;
        a = hornorm;
        horfinal = [horfinal; a];
    end


    verfinal = [];%for y values vertically
    for j = 1:n;
        vernorm = ypos (j) + ydistOP;
        b = vernorm;
        verfinal = [verfinal; b];
    end



    store {i,1,1} = number;
    store {i,2,1} = time;
    store {i,3,1} = horfinal;
    store {i,4,1} = verfinal;
    store {i,5,1} = OPx;
    store {i,6,1} = OPy;
    store {i,7,1} = time_to_arrive;
    store {i,8,1} = reversals;
    store {i,16,1} = omegas;
    
end

% save the data in the designated file
RRF3_Exp30_ZC2834_PA_to_OP_reversals_F1 = [revlab; store ];

